package com.example.collectors;

import com.example.stream.Dish;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangyingjie
 * @date
 * @remark 用流收集数据
 */
public class CollectorsDemo {
    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH) );
        //根据卡路里大小收集
        Comparator<Dish> dishComparator = Comparator.comparingInt(Dish::getCalories);
        Optional<Dish> collect = menu.stream().collect(Collectors.maxBy(dishComparator));
        //汇总
        Integer sum = menu.stream().collect(Collectors.summingInt(Dish::getCalories));
        Integer reduce = menu.stream().map(dish -> dish.getCalories()).reduce(0, Integer::sum);//规约写法
        System.out.println(sum+"=="+reduce);
        //一次性全部汇总
        IntSummaryStatistics summaryStatistics = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
        double average = summaryStatistics.getAverage();
        long count = summaryStatistics.getCount();
        int max = summaryStatistics.getMax();
        int min = summaryStatistics.getMin();
    }
}
